package com.demo.session;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/session2")
public class SessionDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("session2.....");
        /**
         * Session对象由服务器管理（Tomcat管理）
         * 我们在程序中只能通过request对象去获取session，不能自己创建Session
         */
        HttpSession session = req.getSession();
        // 获取session的唯一标识
        String jsessionid = session.getId();
        System.out.println(jsessionid);
        // 获取session中存储数据
        Object username = session.getAttribute("username");
        Object age = session.getAttribute("age");
        Object address = session.getAttribute("address");
        System.out.println(username);
        System.out.println(age);
        System.out.println(address);
        // 将session销毁
        session.invalidate();

        resp.getWriter().write("session2 .session2..");
    }
}
